package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FragmentUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.calculator.unit.constant.ConvertType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.UnitTabAdapter;
import flc.ast.databinding.ActivityUnitCalculatorBinding;
import flc.ast.fragment.UnitConvertFragment;
import stark.common.basic.event.EventStatProxy;

public class UnitCalculatorActivity extends BaseAc<ActivityUnitCalculatorBinding> {

    private UnitTabAdapter mUnitTabAdapter;
    private List<Fragment> mFragmentList;


    @Override
    protected int onCreate() {
        return R.layout.activity_unit_calculator;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivUnitCalculateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvTab.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        mUnitTabAdapter = new UnitTabAdapter();
        mDataBinding.rvTab.setAdapter(mUnitTabAdapter);
        mUnitTabAdapter.setOnItemClickListener(this);
        List<String> titleArr = Arrays.asList(getResources().getStringArray(R.array.unit_title));
        mUnitTabAdapter.setList(titleArr);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mUnitTabAdapter.selIndex = position;
        mUnitTabAdapter.notifyDataSetChanged();
        FragmentUtils.showHide(position, mFragmentList);
    }

    @Override
    protected void initData() {
        getFragmentList();
    }

    private void getFragmentList() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(UnitConvertFragment.newInstance(ConvertType.LENGTH));
        mFragmentList.add(UnitConvertFragment.newInstance(ConvertType.AREA));
        mFragmentList.add(UnitConvertFragment.newInstance(ConvertType.VOLUME));
        mFragmentList.add(UnitConvertFragment.newInstance(ConvertType.WEIGHT));
        mFragmentList.add(UnitConvertFragment.newInstance(ConvertType.TEMPERATURE));
        mFragmentList.add(UnitConvertFragment.newInstance(ConvertType.SPEED));
        mFragmentList.add(UnitConvertFragment.newInstance(ConvertType.TIME));
        FragmentUtils.add(getSupportFragmentManager(), mFragmentList, R.id.flFragment, 0);
    }

}